<?php
/**
 * vim: et ts=4 sts=4 sw=4 fdm=marker
 *
 * 内容管理 
 *
 * @author	wang Penghai <china_php@hotmail.com>
 * @copyright 2008 5e6d Corporation.
 * @version   $Id: config.php 29 2009-03-10 14:30:18Z wph $
 */
pf_load::app_class('admin_base', 'class');
class threads_action extends pf_controller{
	public function __construct()
	{
		parent::__construct();
	}

	//首页列表
	public function index()
	{
		$this->add();
	}

	//添加显示
	public function add()
	{
		$user_obj = pf_load::get_instance('user', 'user');
		$argv = array(
			 'fields'=>array('uid', 'username'),
			 //'limit' => 1,
			 //'offset' => 0,
			 //'order' => 'updatetime desc',
			 //'group'=>'id'
			 //'where'=> '1=1',
		);
		$uid = (int)pf_request::get('uid');
		$fid = (int)pf_request::get('fid');
		$tid = (int)pf_request::get('tid');
		$fid_array = array(
			'4' => '商家。实惠',
			'5' => '冒泡。小店',
			'6' => '衣。时尚',
			'7' => '食。美味',
			'8' => '住。安逸',
			'9' => '宝宝。长大',
			'39' => ' 生活。帮我',
		);

		$user_list = $user_obj->get_user_list($argv);
		$this->assign('title', '您好');
		$this->assign('fid_array', $fid_array);
		$this->assign('user_list', $user_list);
		$this->assign('uid', $uid);
		$this->assign('fid', $fid);
		$this->assign('tid', $tid);
		$this->view('threads_add_tpl.php');
	}
	//插入数据
	public function insert()
	{
		$uid = (int)pf_request::post('uid');
		$fid = (int)pf_request::post('hfid');
		if ($uid !== 0 && $fid !== 0)
		{
			$this->insert_posts();
		} else {
			$this->insert_threads();
		}
	}
	//主题数据入库
	public function insert_threads()
	{
		$argv = $this->post_data();
		$threads_obj = pf_load::get_instance('threads', 'threads');
		$res = $threads_obj->insert_threads($argv);
		$tid = $res['lastinsertid'];
		$ip = '127.0.0.1';
		//入库帖子内容
		$con_argv = array(
			'tid' => $tid,
			'userip' => $ip,
			'ipfrom' => '北京',
			'content' => $content,
			'ifconvert' => 2,
		);
		$threads_obj->insert_tmsgs($con_argv);
		$uid = $argv['authorid'];
		$fid = $argv['fid'];
		echo "<a href='admin.php?c=threads&uid=$uid&fid=$fid&tid=$tid'>继续该主题</a>   <a href='admin.php?c=threads'>发起新主题</a>";
	}
	//回复入库
	protected function insert_posts()
	{
		$content = pf_request::post('content', '', 'html');
		$tid = pf_request::post('tid');
		$userinfo = pf_request::post('userinfo');
		$userinfo = explode('|', $userinfo);
		$uid = $userinfo[0];
		$author = $userinfo[1];
		$fid = pf_request::post('hfid');
	
		$argv = array(
			'author' => $author,
			'authorid' => $uid,
			'fid' => $fid,
			'tid' => $tid,
			'ifcheck' => 1,
			'content' => $content,
		);
		$threads_obj = pf_load::get_instance('threads', 'threads');
		$res = $threads_obj->insert_posts($argv);
		echo "<a href='admin.php?c=threads&uid=$uid&fid=$fid&tid=$tid'>继续该主题</a>   <a href='admin.php?c=threads'>发起新主题</a>";
	}

	protected function post_data()
	{
		$content = pf_request::post('content', '', 'html');
		$title = pf_request::post('title');
		$userinfo = pf_request::post('userinfo');
		$userinfo = explode('|', $userinfo);
		$uid = $userinfo[0];
		$author = $userinfo[1];
		$fid = pf_request::post('fid');
		$time = time();
		$argv = array(
			'fid' => $fid,
			'author' => $author,
			'authorid' => $uid,
			'subject' => $title,
			'postdate' => $time,
			'lastpost' => $time,
			'lastposter' => $author,
			'ifcheck' => 1,
		);
		return $argv;
	}
}
